<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
    <el-form-item label="派单编号" prop="orderno">
        <el-input
          v-model="queryParams.orderno"
          placeholder="请输入派单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
     </el-form-item>
     <el-form-item label="派工类别 " prop="sendtype">
        <el-select v-model="queryParams.sendtype" placeholder="请选择派工类别 " clearable size="small">
          <el-option
            v-for="dict in sendtypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服务状态" prop="servicestatus">
        <el-select v-model="queryParams.servicestatus" placeholder="请选择服务状态" clearable size="small">
          <el-option
            v-for="dict in servicestatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
       <el-form-item label="责任人" prop="membername">
              <el-input
                v-model="queryParams.membername"
                placeholder="请输入责任人"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zyx:sendorder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zyx:sendorder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zyx:sendorder:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sendorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<el-table-column label="派单编号" align="center" prop="orderno" />
      <el-table-column label="单位名称 " align="center" prop="zyxDevice.unitname" />
      <el-table-column label="科室/部门 " align="center" prop="useoffice" :formatter="useofficeshowFormat"/>
      <el-table-column label="设备类别 " align="center" prop="devicetype"/>
      <el-table-column label="设备型号" align="center" prop="zyxDevice.devicemodel" />
      <el-table-column label="派工类别 " align="center" prop="sendtype" :formatter="sendtypeFormat" />
      <!-- <el-table-column label="设备状态" align="center" prop="devicestatus" :formatter="devicestatusFormat" /> -->
      <el-table-column label="服务状态" align="center" prop="servicestatus" :formatter="servicestatusFormat" />
      <el-table-column label="派工时间" align="center" prop="senddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.senddate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进行时间" align="center" prop="processdate" />
      <el-table-column label="责任人 " align="center" prop="membername" />
      <el-table-column label="备注信息" align="center" prop="remark" :show-overflow-tooltip="true" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.servicestatus==='4'&&scope.row.issubmitcost==='Y'"

            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlefj(scope.row)"
            v-hasPermi="['zyx:sendorder:edit']"
          >附加费用</el-button>
          <el-button
          v-if="scope.row.servicestatus==='4'"

            size="mini"
            type="text"
            icon="el-icon-s-flag"
            @click="handlePj(scope.row)"
          >工单评价</el-button>

          <el-button
          v-if="scope.row.servicestatus==='4'"
            size="mini"
            type="text"
            icon="el-icon-printer"
            @click="handlePrint(scope.row)"
          >工单打印</el-button>
        <el-button
          v-if="scope.row.servicestatus==='4'&&scope.row.issubmitcost==='Y'"
            size="mini"
            type="text"
            icon="el-icon-printer"
            @click="handleBXPrint(scope.row)"
          >报销单</el-button>
          <el-button
          v-if="scope.row.servicestatus==='1'"

            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zyx:sendorder:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.servicestatus==='1'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zyx:sendorder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改派工对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="派工类别 " prop="sendtype">
          <el-select v-model="form.sendtype" placeholder="请选择派工类别 ">
            <el-option
              v-for="dict in sendtypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位名称" prop="departid">
          <el-select v-model="form.departid" filterable placeholder="请选择企业" @change="chg()"  disabled="true">
              <el-option
                v-for="item in deptoptions"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId">
              </el-option>
            </el-select>
        </el-form-item>
      <el-form-item label="科室/部门 " prop="useoffice">
        <el-select v-model="form.useoffice" filterable placeholder="请选择科室/部门"  size="small" @change="useofficechg()">
          <el-option
            v-for="dict in useofficeOptions"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId">
            </el-option>
        </el-select>
      </el-form-item>

        <el-form-item label="设备 " prop="deviceid">
          <!-- <el-select v-model="form.deviceid" placeholder="请选择设备 " @change="devicechg()"> -->
            <!-- <el-option
              v-for="dict in deviceOptions"
              :key="dict.id"
              :label="dict.devicemodel"
              :value="dict.id"
            ></el-option> -->

            <el-select   @change="devicechg()"
              style="width: 100%;"
               v-model="form.deviceid"

               filterable
               remote
               reserve-keyword
               placeholder="请选择设备"
               :remote-method="remoteDeviceMethod"
               :loading="loading">
               <el-option
                 v-for="dict in deviceOptions"
                 :key="dict.id"
                 :label="dict.devicemodel"
                 :value="dict.id"
                 >
               </el-option>
             </el-select>


          <!-- </el-select> -->
        </el-form-item>
        <el-form-item label="责任人 " prop="memberid">
         <el-select v-model="form.memberid" placeholder="请选择责任人">
           <el-option
             v-for="dict in memberOptions"
             :key="dict.memberid"
             :label="dict.membername"
             :value="dict.memberid"
           ></el-option>
         </el-select>
        </el-form-item>
      <el-form-item label="信息同步" prop="isshare">
          <el-select v-model="form.isshare" placeholder="请选择信息同步" @change="isrecordchg()">
            <el-option
              v-for="dict in isshareOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="同步人" prop="seladmins" v-show="isshowseladmin">
           <el-select
              v-model="form.seladmins"
              multiple
              filterable
              remote
              reserve-keyword
              placeholder="请选择同步人"
              :remote-method="remoteMethod"
              :loading="loading">
              <el-option
                v-for="item in options"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId">
              </el-option>
            </el-select>
         </el-form-item>

        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"  maxlength="500"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"   v-preventReClick="3000">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



  <repair-print ref="repairPrint" ></repair-print>
  <fj-deail ref="fjDeail" ></fj-deail>

  <pj-deail ref="pjDeail" ></pj-deail>
  <takecare-print  ref="takecarePrint"   ></takecare-print>
  <check-print ref="checkPrint" ></check-print>
  <operate-print ref="operatePrint"  ></operate-print>
  <cost-print  ref="costPrint" ></cost-print>
  </div>
</template>

<script>
import { onlyupd,listSendorder, getSendorder, delSendorder, addSendorder, updateSendorder, exportSendorder } from "@/api/zyx/sendorder";
import { listAllDevice } from "@/api/zyx/device";
import { listAllDept,  } from "@/api/system/dept";
import { listManageMember} from "@/api/zyx/memberDevice";
import{ listadmin }from "@/api/system/user"
import repairPrint from"./repairPrint.vue"
  import {getBillBySendOrderId,} from "@/api/zyx/bill";
  import fjDeail from "./fjDeail.vue"
  import pjDeail from "./pjDeail.vue"
  import takecarePrint from "./takecarePrint.vue"
  import checkPrint from "./checkPrint.vue"
  import operatePrint from "./operatePrint.vue"
  import costPrint from "./costPrint.vue"
export default {
  name: "Sendorder",
  components: {
    repairPrint,
    fjDeail,
    pjDeail,
    takecarePrint,
    checkPrint,
    operatePrint,
    costPrint,
  },
  data() {
    return {
      // 遮罩层
      loading: true,

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      isshowseladmin:false,
      // 总条数
      total: 0,
      // 派工表格数据
      sendorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 派工类别 字典
      sendtypeOptions: [],
      // 设备状态字典
      devicestatusOptions: [],
      // 服务状态字典
      servicestatusOptions: [],
      deptoptions:[],
      useofficeOptions:[],
      deviceOptions:[],
      memberOptions:[],
      devicetypeOptions:[],
      useofficeOptionsshow:[],
      isshareOptions:[],
      options:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        departid: null,
        useoffice:null,
        deviceid: null,
        sendtype: null,
        servicestatus: null,
        processdate: null,
        memberid: null,
        overdate: null,
        isrecord:null,
        orderno:null,
        membername:null,
        orderByColumn:'senddate',
        isAsc:'desc',
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        sendtype: [
          { required: true, message: "派工类别不能为空", trigger: "change" }
        ],
        departid: [
          { required: true, message: "医院不能为空", trigger: "change" }
        ],
        useoffice:[
           { required: true, message: "科室不能为空", trigger: "change" }
        ],
        deviceid: [
          { required: true, message: "设备不能为空", trigger: "change" }
        ],
        memberid: [
          { required: true, message: "责任人不能为空", trigger: "change" }
        ],

        devicestatus: [
          { required: true, message: "设备状态不能为空", trigger: "change" }
        ],
        isshare: [
          { required: true, message: "同步状态不能为空", trigger: "change" }
        ],

      },

    };
  },
  created() {

    this.getList();
    this.getDicts("zyx_sendtype").then(response => {
      this.sendtypeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isshareOptions = response.data;
    });

    this.getDicts("zyx_devicestatus").then(response => {
      this.devicestatusOptions = response.data;
    });
    this.getDicts("zyx_servicestatus").then(response => {
      console.log('zyx_servicestatus',response)
      this.servicestatusOptions = response.data;
    });
    listAllDept({deptType:'1'}).then(response => {
      // console.log(response)
      this.deptoptions = response.data;
    });
 this.getDicts("zyx_devicetype").then(response => {
      this.devicetypeOptions = response.data;
    });
    listAllDept({deptType:'2'}).then(response => {
        // console.log(response)
        this.useofficeOptionsshow = response.data;
      });
  },
  methods: {
    handleBXPrint(row){//报销单

        this.$refs.costPrint.zyxSendorder=row
        this.$refs.costPrint.zyxDevice=row.zyxDevice
        this.$refs.costPrint.getList1();
        this.$refs.costPrint.getList2();
        this.$refs.costPrint.getList3();
        this.$refs.costPrint.getList4();
        this.$refs.costPrint.getList5();
        this.$refs.costPrint.getList6();

        this.$refs.costPrint.getname();
        this.$refs.costPrint.visible = true;
    },
    handlePj(row){//工单评价
      this.$refs.pjDeail.sendorder=row
      this.$refs.pjDeail.getbilldetail();
      this.$refs.pjDeail.visible = true;
      this.$refs.pjDeail.title = "工单评价";
    },
  handlefj(row){//工单附加费用
      this.$refs.fjDeail.form1=row
      this.$refs.fjDeail.id=row.id
      this.$refs.fjDeail.addcost=row.addcost;
      // this.$refs.fjDeail.getbilldetail();
      this.$refs.fjDeail.visible = true;
      this.$refs.fjDeail.title = "附加费用";
    },

    handlePrint(row){
      // console.log(row);
      if(row.sendtype==='1'){
        this.$refs.repairPrint.zyxSendorder=row
        this.$refs.repairPrint.zyxDevice=row.zyxDevice
        this.$refs.repairPrint.getbill(row.id);
        this.$refs.repairPrint.getname();
        this.$refs.repairPrint.visible = true;
      }else if(row.sendtype==='2'){//保养
        this.$refs.takecarePrint.zyxSendorder=row
        this.$refs.takecarePrint.zyxDevice=row.zyxDevice

        this.$refs.takecarePrint.getbill(row.id);
        this.$refs.takecarePrint.getname();
        this.$refs.takecarePrint.visible = true;
      }else if(row.sendtype==='3'){//检定

        this.$refs.checkPrint.zyxSendorder=row
        this.$refs.checkPrint.zyxDevice=row.zyxDevice

        this.$refs.checkPrint.getbill(row.id);
        this.$refs.checkPrint.getname();
        this.$refs.checkPrint.visible = true;
      }else if(row.sendtype==='4'){//运营相关

      this.$refs.operatePrint.zyxSendorder=row
      this.$refs.operatePrint.zyxDevice=row.zyxDevice
      this.$refs.operatePrint.getbill(row.id);
      this.$refs.operatePrint.getname();
      this.$refs.operatePrint.visible = true;

      }

    },
    isrecordchg(){
      // console.log(this.form.isshare)
      if(this.form.isshare=='Y'){
        this.isshowseladmin=true
      }else{
        this.isshowseladmin=false;
      }
    },
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;

          listadmin({userName:query}).then(response => {
              // console.log(response)
              this.options = response.data;
            });
          // this.options = this.list.filter(item => {
          //   return item.label.toLowerCase()
          //     .indexOf(query.toLowerCase()) > -1;
          // });
        }, 200);
      } else {
        this.options = [];
      }
    },
    remoteDeviceMethod(query) {
      if (query !== '') {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;

          // listadmin({userName:query}).then(response => {
          //     // console.log(response)
          //     this.options = response.data;
          //   });

         listAllDevice({departid:this.form.departid,useoffice:this.form.useoffice,devicemodel:query}).then(response => {
           // console.log(response)
           this.deviceOptions = response.data;
         });

        }, 200);
      } else {
        this.options = [];
      }
    },
    chg(){
      this.form.useoffice=null;
      if(this.form.departid==''){
        this.useofficeOptions =[]
      }else{
        listAllDept({deptType:'2',parentId:this.form.departid}).then(response => {
          // console.log(response)
          this.useofficeOptions = response.data;
        });
      }

    },
    useofficechg(){
      this.form.deviceid=null;
      if(this.form.useoffice==''){
        this.deviceOptions =[]
      }else{
        // listAllDevice({departid:this.form.departid,useoffice:this.form.useoffice}).then(response => {
        //   // console.log(response)
        //   this.deviceOptions = response.data;
        // });
      }
    },
    //设备变化
    devicechg(){
      this.form.memberid=null;
      if(this.form.useoffice==''){
        this.memberOptions =[]
      }else{
        listManageMember({deviceid:this.form.deviceid}).then(response => {
          // console.log(response)
          this.memberOptions = response.data;
        });
      }

    },
    /** 查询派工列表 */
    getList() {
      this.loading = true;
      listSendorder(this.queryParams).then(response => {
        this.sendorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否共享字典翻译
    isshareFormat(row, column) {
      return this.selectDictLabel(this.isshareOptions, row.isshare);
    },
    // 派工类别 字典翻译
    sendtypeFormat(row, column) {
      return this.selectDictLabel(this.sendtypeOptions, row.sendtype);
    },
    // 设备状态字典翻译
    devicestatusFormat(row, column) {
      return this.selectDictLabel(this.devicestatusOptions, row.devicestatus);
    },
    // 服务状态字典翻译
    servicestatusFormat(row, column) {
      return this.selectDictLabel(this.servicestatusOptions, row.servicestatus);
    },
    // 设备类别字典字典翻译
    devicetypeFormat(row, column) {
      return this.selectDictLabel(this.devicetypeOptions, row.devicetype);
    },
    // 使用科室字典字典翻译
    useofficeshowFormat(row, column) {
      // console.log(this.selectDeptLabel(this.useofficeOptions, row.useoffice))
      return this.selectDeptLabel(this.useofficeOptionsshow, row.useoffice);
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        departid: null,
        deviceid: null,
        sendtype: null,
        devicestatus: null,
        servicestatus: null,
        senddate: null,
        processdate: null,
        memberid: null,
        overdate: null,
        useoffice:null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        isshare:null,
        seladmins:null,
        orderno:null
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {

      // console.log('登录用户信息',this.$store.state.user)

      this.reset();
      this.form.departid=this.$store.state.user.deptid;
      this.chg();
      this.open = true;
      this.title = "添加派工";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSendorder(id).then(response => {
        this.form = response.data;

        listAllDept({deptType:'2',parentId:response.data.departid}).then(response => {
          this.useofficeOptions = response.data;
        });
        listAllDevice({departid:this.form.departid,useoffice:this.form.useoffice}).then(response => {
          this.deviceOptions = response.data;
        });
        listManageMember({deviceid:this.form.deviceid}).then(response => {
          this.memberOptions = response.data;
        });
        this.isrecordchg()

        this.form.seladmins=response.selids;
        this.options=response.seladmins;
        this.open = true;
        this.title = "修改派工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.isshare=="Y"){
            if(this.form.seladmins==''){
              this.msgError("请选择信息共享人")
              return;
            }
          }
          if (this.form.id != null) {
            updateSendorder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSendorder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除派工信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSendorder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有派工数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSendorder(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
